<?php

namespace app\cps\controller;

use app\cps\service\VideoService;
use app\model\VideoDetails;
use think\facade\Request;
use app\common\Common;
use app\common\Error;

class Video extends Base
{
    private $service;

    public function __construct(VideoService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染短剧列表页
    public function index()
    {
        return view();
    }

    // 渲染短剧剧集列表页
    public function info()
    {
        return view();
    }

    // 短剧列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getShortPlayList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 指定短剧的剧集列表
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getDramaList($params['video_id'], $this->getUserInfo());

        return Common::success($data);
    }

    // 设置/取消关注剧集
    public function setFollowDrama()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('set_follow_drama')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->setFollowDrama($params['video_detail_id'], $this->getUserInfo());

        if (!empty($result['message'])) {
            return Common::fail($result['message']);
        }

        return Common::success();
    }

    // 短剧下拉列表
    public function select()
    {
        $params = Request::param();
        $data = $this->service->getShortPlaySelectList($params, $this->getUserInfo());

        return Common::success($data);
    }

    /**
     * 渲染自定义短剧收费配置列表页面
     */
    public function configIndex()
    {
        return view();
    }

    /**
     * 渲染自定义短剧收费配置详情页面
     */
    public function configInfo()
    {
        return view();
    }

    /**
     * 自定义短剧收费配置列表
     */
    public function configList()
    {
        $params = Request::param();

        $data = $this->service->getCustomChargeConfigureList($params, $this->getUserInfo());

        return Common::success($data);
    }

    /**
     * 自定义短剧收费配置详情
     */
    public function configDetail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('config_detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getCustomChargeConfigureDetail($params['id']);

        return Common::success($data);
    }

    /**
     * 添加自定义短剧收费配置
     */
    public function addConfig()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('add_config')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomChargeConfigure($params, $this->getUserInfo());

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    /**
     * 编辑自定义短剧收费配置
     */
    public function editConfig()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('edit_config')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomChargeConfigure($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 短剧收费设置下拉列表
    public function paySelect()
    {
        $params = Request::param();
        $data = $this->service->paySettingSelect($params, $this->getUserInfo());

        return Common::success($data);
    }

    /**
     * 删除自定义短剧收费配置
     */
    public function deleteConfig()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('delete_config')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteCustomChargeConfigure($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染智能推送设置页面
    public function set()
    {
        $data = $this->service->getPushSetting($this->getUserInfo());
        return view('', ['data' => $data]);
    }

    // 智能推送设置
    public function setting()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('setting')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->pushSetting($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 短剧详情页面
    public function detailInfo()
    {
        return view();
    }

    // 短剧详情数据
    public function detailCharge()
    {
        $params = Request::param();
        $data = $this->service->getDetailCharge($params);
        return Common::success($data);
    }

    // 批量设置短剧自定义收费
    public function batchSettingPay()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('batch_setting_pay')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->batchSettingVideoPay($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success();
    }

    // 视频下载
    public function downloadVideo()
    {
        $params = Request::param();
        $id = $params['id'] ?? 0;
        if ($id){
            $videoDetail = VideoDetails::where('id',$id)->find();
            if ($videoDetail){
                $showName = ($videoDetail->video->name ?? '').'-'.$videoDetail->name;
                header('Content-Type: application/octet-stream');
                header("Content-transfer-encoding: Binary");
                header('Content-Disposition: attachment; filename="' . $showName.'.mp4' . '"');
                readfile($videoDetail->third_url);
                exit;
            }
        }
        return Common::fail();
    }
}